export default {
  namespaced: true,
  state: {
    user: null,
    routesConfig: null,
  },
  getters: {
    user: state => {
      if (!state.user) {
        let user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
        user = user ? JSON.parse(user) : {}
        state.user = user
      }
      return state.user
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        let routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY)
        routesConfig = routesConfig ? JSON.parse(routesConfig) : []
        state.routesConfig = routesConfig
      }
      return state.routesConfig
    }
  },

  mutations: {
    setUser(state, user) {
      state.user = user
      if (user) {
        localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify(user));
      } else {
        localStorage.removeItem(process.env.VUE_APP_USER_KEY)
      }
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig
      if (routesConfig) {
        localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig))
      } else {
        localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
      }
    }
  }
}
